﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO.Ports;
namespace BTServer
{
    public partial class Form1 : Form
    {
        private List<Device> devices = new List<Device>();
        private BackgroundWorker bg;
        private Guid service = new Guid("{7A51FDC2-FDDF-4c9b-AFFC-98BCD91BF93B}");
        private SerialPort port;
        private InTheHand.Net.Sockets.BluetoothDeviceInfo info;
        public Form1()
        {
            InitializeComponent();
            bg = new BackgroundWorker();
            bg.DoWork +=new DoWorkEventHandler(bg_DoWork);
            bg.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bg_RunWorkerCompleted);
            
        }

        void bg_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            comboBox1.DataSource = (List<Device>)e.Result;
            progressBar1.Visible = false;
            button1.Enabled = true;
            if(comboBox1.SelectedIndex != -1)
                button2.Enabled = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!bg.IsBusy)
            {
                bg.RunWorkerAsync();
                progressBar1.Visible = true;
                button1.Enabled = false;
                button2.Enabled = false;
            }
        }
        void bg_DoWork(object sender, DoWorkEventArgs e)
        {
            InTheHand.Net.Sockets.BluetoothClient bc = new InTheHand.Net.Sockets.BluetoothClient();
            InTheHand.Net.Sockets.BluetoothDeviceInfo[] array = bc.DiscoverDevices();
            int count = array.Length;
            for (int i = 0; i < count; i++)
            {
                Device device = new Device(array[i]);
                devices.Add(device);
            }
            e.Result = devices;
        }
        

        private void button2_Click(object sender, EventArgs e)
        {
            string [] portNames;
            InTheHand.Net.Bluetooth.BluetoothSecurity.PairRequest(devices[comboBox1.SelectedIndex].Address, "1234");
            info = new InTheHand.Net.Sockets.BluetoothDeviceInfo(devices[comboBox1.SelectedIndex].Address);
            info.SetServiceState(InTheHand.Net.Bluetooth.BluetoothService.SerialPort, true);
            portNames = SerialPort.GetPortNames();
            port = new SerialPort("COM6");
            MessageBox.Show(devices[comboBox1.SelectedIndex].DeviceName + " is connected", "Connection established", MessageBoxButtons.OK);
            port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);

            if (!port.IsOpen)
            {
                port.Open();
            }
        }

        int readedChar;
        void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            SerialPort sp = (SerialPort)sender;
            readedChar = sp.ReadChar();

            textBox2.Invoke((MethodInvoker)delegate()
            {
                textBox2.Text += Convert.ToChar(readedChar) ;
            }
            );
        }

        private void button3_Click(object sender, EventArgs e)
        {
            char []array = new char[1];
            if (!port.IsOpen)
            {
                port.Open();
            }
            textBox2.Text += port.ReadChar();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            info.SetServiceState(InTheHand.Net.Bluetooth.BluetoothService.SerialPort, false);
        }
    }
}
